<?php

namespace App\Http\Controllers;

use App\Models\Chat;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Storage;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class UserController extends Controller
{
    public function __construct(){
        $this->middleware('auth');
    }

    public function index()
    {
        $user = User::orderBy('id')->paginate(8);
        $role = Role::all();
        $permission= Permission::all();
        $gender = ['0'=>'不详','1'=>'男','2'=>'女'];
        foreach ($user as $v){
            $v->sex=$gender[$v->sex];
        }
        /*foreach ($role as $v){
            $diff = $permisson->diff($v->permissions);
        }*/

        return view('user',[
            'user'=>$user,
            'role'=>$role,
            'permission'=>$permission
        ]);

    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = User::find($id);
        return response()->json([
            'user' => $user,
        ]);
    }

    public function rightStore(){
        $user_id = Input::get('user_id');
        $role_id = Input::get('role_id');
        $ids = Input::get('ids');
        $user = User::find($user_id);
        if($role_id){
            $user_get_role = $user->syncRoles($role_id);
        }else{
            $user_get_role = $user->syncRoles();
        }
        if($ids){
            $user_get_per = $user->syncPermissions($ids);
        }else{
            $user_get_per = $user->syncPermissions();
        }
    }

    public function changePermission(){
        $id = Input::get('id');
        $permission = Permission::all();
        $role= Role::findById($id);
        $diff = $permission->diff($role->permissions);
        return response()->json($diff);
    }

    public function rightEdit(){
        $id=Input::get('id');
        $user = User::find($id);
        $permission = Permission::all();
        $role_name = $user->getRoleNames();
        $diff=$permission;
        $role_id=-1;
        if(!empty($role_name)&&count($role_name)>0){
            $role = Role::findByName($role_name[0]);
            $role_id=$role->id;
            $diff = $permission->diff($role->permissions);
        }
        $permissions =$user->getDirectPermissions();
        return response()->json([
            'role_id'=>$role_id,
            'permissions'=>$permissions,
            'diff'=>$diff
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $input = $request->all();
        if(Input::ajax()){
            $messages = [
                'required' => ':attribute值不能为空',
                'unique' => ':attribute已存在',
                'email'=>'电子邮件必须是有效的电子邮件地址'
            ];
            $this->validate($request, [
                'name' => 'bail|required|unique:users,name,'.$id.'|max:255',
                'email' => 'bail|required|unique:users,email,'.$id.'|email|max:25',
            ], $messages);
            $user = User::find($id);
            $user->name = $input['name'];
            $user->email = $input['email'];
            $user->save();
            return response()->json([
                'user' => $user
            ]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $input = Input::get('id');
        if(Input::ajax()) {
            $user = User::find($id);
            $boole = Storage::delete('public/headpic/'.$user->headpic);
            Chat::withTrashed()->where('user_id',$id)->forceDelete();
            $user->delete();
            return response()->json([
                'user' => 'public/headpic/'.$user->headpic,
                'boole'=>$boole
            ]);
        }
    }
}
